< previous page page_102 next page >

Page 102
Private Sub Form_Click()
    Command1_Click
End Sub
Run the program, and you will notice that whether you click the form or the command button, the same MsgBox statement is executed, just as if the user had clicked the command button itself.
note.gif If the event procedure you are calling has parameters, such as the MouseMove event, you have to supply them when calling the event procedure. square.gif

Understanding Event Sequences
By now, you should have an understanding of what events are and how Visual Basic handles them. You have seen how to write code to take action when an event occurs. But you need to dive just a little deeper into the world of events.
As you learned earlier, a single user action or system event can trigger multiple events. This result can be good because you can use these different events to handle different situations, as in the case of the MouseDown and Click events. However, there is a flip side. (Isn't there always?) If you write code for multiple events that can occur, these procedures can interact in ways that you don't want. In the worst case, a sequence of eventseach with its own event procedurecan put your system into an infinite loop. The following are several keys to avoiding these problems:
Recognize that multiple events can occur.
Determine which events occur for a user action.
Understand the sequence in which the events occur.
Test the interactions between multiple events in your code.
In the following sections, you examine some issues to be aware of when writing event procedures in Visual Basic.
Multiple Events for Each Action
One of the problems of handling multiple events is that the user can interact with your program in a very large number of ways. For example, did the user move to the text box with a mouse or with the Tab key? Prior to the move, was the focus on another text box or was it on a command button? And what happens when you move from one form to another? As you can see, the possibilities can be almost overwhelming.
The good news, though, is that you will not write code for most object/event combinations. Therefore, even though these events might occur, your program ignores themand they do not cause you any problems.

 
< previous page page_102 next page >